package callgraph;

import callgraph.domain.CallGraphConfig;
import callgraph.domain.JarConfig;
import callgraph.process.CallGraphPipeline;
import org.yaml.snakeyaml.Yaml;
import org.yaml.snakeyaml.constructor.Constructor;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import static callgraph.process.CallGraphProcessStorage.INIT_SIZE_1000;

public class StaticCallGraph {

    public static void main(String[] args) {
        StaticCallGraph staticCallGraph = new StaticCallGraph();
        staticCallGraph.run(args);
    }

    private void run(String[] args) {
        CallGraphConfig callGraphConfig = loadYaml();
        // TODO: 2021/11/14 关联到其他jar包。 need refactor
        Map<String, Set<String>> calleeMethodMapGlobal = new HashMap<>(INIT_SIZE_1000);
        for (JarConfig jarConfig : callGraphConfig.getJarConfigs()) {
            try {
                CallGraphPipeline.handle(jarConfig, calleeMethodMapGlobal);
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private CallGraphConfig loadYaml() {
        Constructor constructor = new Constructor(CallGraphConfig.class);
        Yaml yaml = new Yaml(constructor);
        InputStream inputStream = this.getClass().getClassLoader().getResourceAsStream("callgraph.yaml");
        return yaml.load(inputStream);
    }
}
